package com.gpay.erp.services.purchase.impl;

import com.gpay.erp.core.dao.BaseDao;
import com.gpay.erp.core.dao.page.PagerModel;
import com.gpay.erp.services.common.PaytypeService;
import com.gpay.erp.services.common.SystemCommonService;
import com.gpay.erp.services.common.impl.StringUtil;
import com.gpay.erp.services.purchase.*;
import com.gpay.erp.services.purchase.bean.*;
import com.gpay.erp.web.util.Arith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by wuzonghui on 15/11/13.
 */
@Service
public class PaymentOrderServiceImpl implements PaymentOrderService {
    @Resource
    private BaseDao dao;
    @Autowired
    private SystemCommonService systemCommonService;
    @Autowired
    private PaymentOrderSubService paymentOrderSubService;
    @Autowired
    private PaidApplicationService paidApplicationService;
    @Autowired
    private PaidApplicationSubService paidApplicationSubService;
    @Autowired
    private ContractService contractService;

    @Override
    public String getPaymentOrderNumber() {

        String strCode = systemCommonService.saveSelectByName("PAYMENTORDER");
        return strCode;    }

    @Override
    public int insert(PaymentOrder paymentOrder) {
        return dao.insert("paymentOrder.insert",paymentOrder);
    }

    @Override
    public int delete(PaymentOrder paymentOrder) {
        return 0;
    }

    @Override
    public int deletes(String[] ids) {
        return 0;
    }

    @Override
    public int update(PaymentOrder paymentOrder) {
        return 0;
    }

    @Override
    public PaymentOrder selectOne(PaymentOrder paymentOrder) {
        return null;
    }

    @Override
    public PaymentOrder selectById(String id) {
        return (PaymentOrder) dao.selectOne("paymentOrder.selectById",id);
    }

    @Override
    public PagerModel selectPageList(PaymentOrder paymentOrder) {
        return null;
    }

    @Override
    public List<PaymentOrder> selectList(PaymentOrder paymentOrder) {
        return null;
    }

    /**
     * 插入付款单主表和子表
     * 修改付款申请表的付款单号和相关信息
     * 付款申请主表:实付金额,说明
     * 付款申请子表:1.实付金额和付款金额相等
     * 2.实付金额<申请金额  =实付金额
     * 修改付款申请子表信息
     * 修改合同信息
     * @param paymentOrder
     * @param paymentOrderSubs
     * @return
     */
    @Override
    public int savePaymentOrderSubs(PaymentOrder paymentOrder, List<PaymentOrderSub> paymentOrderSubs) {
        int insertId = 1;
        insertId = insert(paymentOrder); //插入付款单主表
        for (PaymentOrderSub paymentOrderSub:paymentOrderSubs) { //插入付款单子表
            paymentOrderSub.setF_paymentOrderId(String.valueOf(insertId));
            paymentOrderSubService.insert(paymentOrderSub);
        }
        PaidApplication paidApplication = paidApplicationService.selectById(paymentOrder.getF_paidId());
        if(paidApplication != null) {
            paidApplication.setPaidNumber(paymentOrder.getPaidNumber());
            paidApplication.setPayPerson(paymentOrder.getPayPerson());
            paidApplication.setPayDate(StringUtil.getFormateTime());
            paidApplication.setF_paymentOrderId(String.valueOf(insertId));
            paidApplication.setRealAmount(paymentOrder.getRealAmount());
            paidApplication.setCurrencyCode(paymentOrder.getCurrencys());
            paidApplication.setPaidStatus("5");
            paidApplication.setF_paymentOrderId(String.valueOf(insertId));
        }
        paidApplicationService.update(paidApplication);//修改付款申请相关信息

        PaidApplicationSub paidApplicationSub = new PaidApplicationSub();
        paidApplicationSub.setF_paidId(paidApplication.getId());
        List<PaidApplicationSub> paidApplicationSubs = paidApplicationSubService.selectList(paidApplicationSub);
        Double realAmount = paymentOrder.getRealAmount(); //主表实付金额
        int countLast=1;
        for (PaidApplicationSub paidApplicationSub1:paidApplicationSubs) {
            Double appAmount = paidApplicationSub1.getAppBalance();
            if (countLast == paidApplicationSubs.size()) { //实付金额>申请金额, 最后一条数据
                if (realAmount > appAmount||realAmount == appAmount){
                    paidApplicationSub1.setPaidAmount(realAmount); //主表剩下实付金额
                }else {
                    paidApplicationSub1.setPaidAmount(realAmount); //主表剩下实付金额
                }
            }else {
                if (realAmount > appAmount||realAmount == appAmount){
                    paidApplicationSub1.setPaidAmount(appAmount); //子表实付金额
                    if (Arith.sub(realAmount,appAmount)>0){
                        realAmount = Arith.sub(realAmount,appAmount); //主表剩下实付金额
                    }
                }else {
                    paidApplicationSub1.setPaidAmount(realAmount); //主表实付金额不够
                    realAmount = 0.0;
                }

            }

            paidApplicationSubService.update(paidApplicationSub1);
            Contract contract = contractService.selectById(paidApplicationSub1.getF_contractId());
            contract.setAmountPaid(paidApplicationSub1.getPaidAmount());//修改合同实付金额
            contract.setAmountApplication(Arith.sub(contract.getAmountApplication(),paidApplicationSub1.getAppBalance()));
            contractService.update(contract);
            countLast++;
        }
        return insertId;
    }

    @Override
    public int saveUpdatePaymentOrderSubs(PaymentOrder paymentOrder, PaymentOrder paymentOrder1, PaidApplication paidApplication, List<PaidApplicationSub> paidApplicationSubs, List<Contract> contracts) {
        int updateId = 1;
        updateId = update(paymentOrder);//被红冲
        List<PaymentOrderSub> paymentOrderSubs = paymentOrderSubService.selectListByFid(String.valueOf(updateId));
        int insertRedId = insert(paymentOrder1);//红冲插入
        for (PaymentOrderSub paymentOrderSub:paymentOrderSubs) {
            paymentOrderSub.setPayAmount(-paymentOrderSub.getPayAmount());
            paymentOrderSub.setF_paymentOrderId(String.valueOf(insertRedId));
            paymentOrderSubService.insert(paymentOrderSub);
        }
        paidApplicationService.update(paidApplication);
        for (PaidApplicationSub paidApplicationSub:paidApplicationSubs) { //修改付款申请子项实付金额
            paidApplicationSubService.update(paidApplicationSub);
        }
        for (Contract contract:contracts){ //修改合同实付金额
            contractService.update(contract);
        }
        return updateId;
    }
}
